package com.wave.wavesystem.business.aop;


import com.wave.wavesystem.business.annotation.AuthCheck;
import com.wave.wavesystem.business.pojo.emums.user.UserRoleEnum;
import com.wave.wavesystem.business.pojo.entity.UserEntity;
import wave.common.pojo.enums.RStatusEnum;
import com.wave.wavesystem.common.util.AuthUtil;
import com.wave.wavesystem.common.util.Func;
import com.wave.wavesystem.common.util.ThrowUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;


/**
 * @author 夕下风波
 * @date 2025-02-03 10:26
 * @description
 */
@Aspect
@Component
public class AuthCheckInterceptor {

    @Around("@annotation(authCheck)")
    public Object doAuthCheck(ProceedingJoinPoint joinPoint, AuthCheck authCheck) throws Throwable {
        UserRoleEnum userRoleEnum = authCheck.mastRole();
        UserEntity user = AuthUtil.getUser();
        ThrowUtil.throwIf(user == null, RStatusEnum.AUTH_FAIL);
        ThrowUtil.throwIf(Func.strNotContains(user.getRoles(), userRoleEnum.getCode()), RStatusEnum.AUTH_FAIL);
        return joinPoint.proceed();
    }

}
